package com.migu.spms.webapi;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.migu.spms.dao.entity.FunctionRequireCriteria;
import com.migu.spms.dao.entity.ProjectCriteria;
import com.migu.spms.dao.entity.ProjectDO;
import com.migu.spms.dao.entity.TestBaseLineCriteria;
import com.migu.spms.dao.entity.TestBaseLineDO;
import com.migu.spms.dao.inf.IProjectDO;
import com.migu.spms.dao.inf.ITestBaseLineDO;
import com.migu.spms.service.data.BaseLineVersion;
import com.migu.spms.service.impl.FunctionCommonServiceImpl;
import com.migu.spms.webapi.data.BaseLineVersionRequest;
import com.migu.spms.webapi.data.BaseLineVersionResponse;
import com.migu.spms.webapi.data.EditRequireRequest;
import com.migu.spms.webapi.data.ProjectResponse;
import com.migu.spms.webapi.data.TestBaseLineResponse;

/**
 * 
* 项目名称:  SPMS需求管理
* 包:        com.migu.spms.webapi 
* 类名称:    CreateRequireController.java
* 类描述:    新建需求控制类
* 创建人:    fw  
* 创建时间:  2017年6月13日 下午4:29:02
 */
// @codeCreate.restfulAPICreate.util.annotation.type.Controller(introduction =
// "新建需求")
@RestController
@RequestMapping(value = "/common", produces = "application/json;charset=UTF-8")
public class FunctionCommonController extends BaseController {

	@Resource(name = "functionCommonServiceImpl")
	private FunctionCommonServiceImpl functionCommonService;
	
	@Resource(name="ITestBaseLineDO")
	private ITestBaseLineDO testBaseLineDao;
	
	@Resource(name="IProjectDO")
	private IProjectDO projectDao;

	/**
	* @Title: queryAvailableBaseline
	* @Description: 查询可用基线版本号
	* @param @param req
	* @param @param resp
	* @param @param sessionId
	* @param @return
	* @param @throws Exception  
	* @return String
	* @author le
	* @throws
	*/ 
	@RequestMapping(value = "/queryAvailableBaseline")
    public String queryAvailableBaseline(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception {
//		com.migu.spms.service.data.User user=getUserBySession(req, sessionId);
//		if(null==user){
//			return generateResponse(401);
//		}
		BaseLineVersionRequest baseLineRequest = ParseMsg(req, BaseLineVersionRequest.class);
		baseLineRequest.setProject_id(baseLineRequest.getProjectId());
		List<BaseLineVersion> baseLineResponse=functionCommonService.queryBaselineVersion(baseLineRequest,sessionId);
		BaseLineVersionResponse baseLineVersionResponse=new BaseLineVersionResponse();
		baseLineVersionResponse.setBaseLineList(baseLineResponse);
		return generateResponse(baseLineVersionResponse,200);
	}
	
	/**
	* @Title: queryVersionNumberList
	* @Description: TODO
	* @param @param req
	* @param @param resp
	* @param @param sessionId
	* @param @return  
	* @return String
	* @author le
	* @throws
	*/ 
	@RequestMapping(value = "/queryVersionNumberList")
	public String queryVersionNumberList(HttpServletRequest req, HttpServletResponse resp, String sessionId){
		com.migu.spms.service.data.User user=getUserBySession(req, sessionId);
		if(null==user){
			return generateResponse(401);
		}
		TestBaseLineCriteria record=ParseMsg(req,TestBaseLineCriteria.class);
		try {
			addPageQuery(record);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<TestBaseLineDO> et=testBaseLineDao.queryVersionNumberList(record);
		TestBaseLineResponse response=new TestBaseLineResponse();
		response.setResult(et);
		return generateResponse(response,200);
	}
	
	/**
	* @Title: deleteRequirement
	* @Description: 删除需求接口
	* @param @param req
	* @param @param resp
	* @param @param sessionId
	* @param @return  
	* @return String
	* @author le
	* @throws
	*/ 
	@RequestMapping(value = "/deleteRequirement")
	public String deleteRequirement(HttpServletRequest req, HttpServletResponse resp, String sessionId){
		FunctionRequireCriteria record=ParseMsg(req,FunctionRequireCriteria.class);
		functionCommonService.deleteRequirement(record.getId());
		return generateResponse(200);
	}
	
	@RequestMapping(value = "/updateScheduleInfo")
	public String updateScheduleInfo(HttpServletRequest req, HttpServletResponse resp, String sessionId) throws Exception{
		com.migu.spms.service.data.User user=getUserBySession(req, sessionId);
		if(null==user){
			return generateResponse(401);
		}
//		com.migu.spms.service.data.User user=new com.migu.spms.service.data.User();
//		user.setUserId(1);
		EditRequireRequest record=ParseMsg(req,EditRequireRequest.class);
		functionCommonService.updateScheduleInfo(record,user);
		return generateResponse(200);
	}
	
	@RequestMapping(value = "/queryProjectList")
	public String queryProjectList(HttpServletRequest req, HttpServletResponse resp, String sessionId){
		ProjectCriteria record=ParseMsg(req,ProjectCriteria.class);
		List<ProjectDO> projectDOS=projectDao.selectByCriteria(record);
		ProjectResponse projectResponse=new ProjectResponse();
		projectResponse.setResult(projectDOS);
		return generateResponse(projectResponse,200);
	}
	//查询所有创建需求的基线版本号
	@RequestMapping(value = "/queryAllBaseVersion")
	public String queryAllBaseVersion(HttpServletRequest req, HttpServletResponse resp, String sessionId){
		ProjectCriteria record=ParseMsg(req,ProjectCriteria.class);
		List<BaseLineVersion> baseLineResponse=functionCommonService.queryAllBaseVersion(record);
		BaseLineVersionResponse baseLineVersionResponse=new BaseLineVersionResponse();
		baseLineVersionResponse.setBaseLineList(baseLineResponse);
		return generateResponse(baseLineVersionResponse,200);
	}
	/**
	 * 模糊查询添加条件
	 */
	private void addPageQuery(TestBaseLineCriteria testBaseLineCriteria) throws Exception {
		int pageNo = testBaseLineCriteria.getPageNo();
		int pageSize = testBaseLineCriteria.getPageSize();
		int rowStar = (pageNo - 1) * pageSize;
		int rowEnd = pageNo * pageSize - 1;
		testBaseLineCriteria.setRowSrt(rowStar);
		testBaseLineCriteria.setRowEnd(rowEnd);
	}
	
}
